<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_SummaryMeshJobService"></title>
    <script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
    <style>
        .ol-popup {
            position: absolute;
            top: 10px;
            right: 10px;
        }
    </style>
</head>

<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
    <div id="map" style="width: 100%;height:100%"></div>
    <div id="popup" class="ol-popup" style='width:350px'>
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title" data-i18n="resources.title_SummaryMeshJobService"></h3>
            </div>
            <div class="panel-body">
                <div class="input-group">
                    <span class="input-group-addon"><span data-i18n="resources.text_inputData"></span><span data-i18n="[title]resources.text_requiredField"
                            style="color: red;"> * </span>　</span>
                    <input id="datasetName" type="text" class="form-control" value="samples_newyork_taxi_2013-01_14k" />
                </div>
                <p>
                    <div class="input-group">
                        <span class="input-group-addon"><span data-i18n="resources.text_polymerizationType"></span><span
                                data-i18n="[title]resources.text_requiredField" style="color: red;"> * </span>　</span>
                        <select class="form-control" id="type" name="clientType">
                            <option value="SUMMARYMESH" selected="selected" data-i18n="resources.text_polymerizationMesh"></option>
                            <option value="SUMMARYREGION" data-i18n="resources.text_polymerizationRegion"></option>
                        </select>
                    </div>
                    <p>
                        <div id="summarymeshS">
                            <div class="input-group">
                                <span class="input-group-addon"><span data-i18n="resources.text_meshSurfaceType"></span><span
                                        data-i18n="[title]resources.text_requiredField" style="color: red;"> * </span></span>
                                <select class="form-control" id="meshType" name="clientType">
                                    <option value="0" selected="selected" data-i18n="resources.text_4grid"></option>
                                    <option value="1" data-i18n="resources.text_6grid"></option>
                                </select>
                            </div>
                            <p>
                                <div class="input-group">
                                    <span class="input-group-addon" data-i18n="resources.text_analysisRange"></span>
                                    <input id="query" type="text" class="form-control" value="-74.150, 40.550, -73.750, 40.950" />
                                </div>
                                <p>
                                    <div class="input-group">
                                        <span class="input-group-addon"><span data-i18n="resources.text_resolution"></span><span
                                                data-i18n="[title]resources.text_requiredField" style="color: red;"> *
                                            </span>　　</span>
                                        <input id="resolution" type="text" class="form-control" value="100" />
                                    </div>
                                    <p>
                        </div>
                        <div id="summaryregionS" style="display: none">
                            <div class="input-group">
                                <span class="input-group-addon" data-i18n="resources.text_regionDataset"></span>
                                <select class="form-control" id="regionDataset" name="clientType">
                                    <option value="samples_processing_newyorkZone_R" selected="selected">
                                        samples_processing_newyorkZone_R
                                    </option>
                                    <option value="samples_processing_singleRegion_R">samples_processing_singleRegion_R
                                    </option>
                                </select>
                            </div>
                            <p>
                        </div>
                        <div class="input-group">
                            <span class="input-group-addon" data-i18n="resources.text_statisticModes"></span>
                            <input id="statisticModes" type="text" class="form-control" value="max" />
                        </div>
                        <p>
                            <div class="input-group">
                                <span class="input-group-addon" data-i18n="resources.text_weightField"></span>
                                <input id="fields" type="text" class="form-control" value="col7" />
                            </div>
                            <p>
                                <div align="right">
                                    <input type="button" id='btn' class="btn btn-primary" data-i18n="[value]resources.btn_polymerization" />
                                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" include="bootstrap,jquery,widgets" src="../js/include-web.js"></script>
    <script type="text/javascript">
        var host = window.isLocal ? window.server : "https://iserver.supermap.io";
        var layer,
            processingsUrl = host + "/iserver/services/distributedanalyst/rest/v1/jobs",
            mapURL = host + "/iserver/services/map-world/rest/maps/World",
            map = new ol.Map({
                target: 'map',
                // ol v7版本用法为ol.control.defaults.defaults； v6版本以下用法为ol.control.defaults
                controls: ol.control.defaults.defaults({
                        attributionOptions: {
                            collapsed: false
                        }
                    })
                    .extend([new ol.supermap.control.Logo({ link: "https://iclient.supermap.io" })]),
                view: new ol.View({
                    center: [-73.95, 40.75],
                    zoom: 12,
                    projection: 'EPSG:4326',
                    multiWorld: true
                })
            });
        map.addLayer(new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest({
                url: mapURL,
            }),
        }));
        $("#type").change(function (e) {
            if (e.target.selectedIndex === 0) {
                $("#summarymeshS").show();
                $("#summaryregionS").hide();
                return;
            }
            $("#summarymeshS").hide();
            $("#summaryregionS").show();
        });
        var processingService = new ol.supermap.ProcessingService(processingsUrl, {
                withCredentials: window.isLocal
            }),
            info = new ol.control.Control({
                element: document.getElementById('popup')
            });

        info.setMap(map);
        map.addControl(info);
        ol.supermap.SecurityManager.registerToken(processingsUrl, window.exampleToken);

        function getQuery() {
            if ($('#query').val() === "") {
                return "";
            }
            var query = [];
            $('#query').val().split(',').map(function (el) {
                query.push(parseFloat(el));
            });
            return query;
        }

        $('#btn').on('click', function () {
            if ($('#msg_container')[0]) {
                $('#msg_container').remove();
            }
            widgets.loader.showLoader();
            if (map && layer) {
                map.removeLayer(layer);
            }
            var summaryMeshJobParameter = new ol.supermap.SummaryMeshJobParameter({
                datasetName: $('#datasetName').val(),
                resolution: $('#resolution').val(),
                meshType: $('#meshType option:selected').attr('value'),
                fields: $('#fields').val(),
                query: getQuery(),
                statisticModes: $('#statisticModes').val(),
                type: $('#type option:selected').attr('value'),
                regionDataset: $('#regionDataset option:selected').attr('value')
            });
            processingService.addSummaryMeshJob(summaryMeshJobParameter).then(function (serviceResult) {
                if (serviceResult.error) {
                    widgets.loader.removeLoader();
                    var errorMsg = serviceResult.error.errorMsg || "code: " + serviceResult.error.code;
                    widgets.alert.showAlert(resources.msg_createFailed + "<br>" + errorMsg, false);
                    return;
                }
                serviceResult.result.setting.serviceInfo.targetServiceInfos.map(function (info) {
                    if (info.serviceType === 'RESTMAP') {
                        ol.supermap.FetchRequest.get(info.serviceAddress + '/maps').then(function (
                            response) {
                            return response.json();
                        }).then(function (result) {
                            var mapUrl = result[0].path;
                            new ol.supermap.MapService(mapUrl).getMapInfo().then(function (
                                mapInfo) {
                                layer = new ol.layer.Tile({
                                    source: new ol.source.ImageSuperMapRest(
                                        ol.source.ImageSuperMapRest
                                        .optionsFromMapJSON(mapUrl,
                                            mapInfo.result))
                                });
                                map.addLayer(layer);
                                widgets.loader.removeLoader();
                            });
                        });
                    }
                });
            });
        });
    </script>
</body>

</html>